﻿using NetCoreBBS.Entities;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace NetCoreBBS.ViewModels
{
    public class ApplicationUserViewModel
    {
        public string Id { get; set; }

        [Display(Name = "姓名")]
        public string Name { get; set; }

        [Display(Name = "用户名")]
        public string UserName { get; set; }

        [Display(Name = "邮件")]
        public string Email { get; set; }

        [Display(Name = "手机号")]
        public string PhoneNumber { get; set; }

        [Display(Name = "角色")]
        public IList<string> RoleNames { get; set; }

        [Display(Name = "单位")]
        public int DepartmentId { get; set; }

        [Display(Name = "单位")]
        public string DepartmentName { get; set; }


        [Display(Name = "创建时间")]
        public DateTime CreateTime { get; set; }

        [Display(Name = "最后登录时间")]
        public DateTime LastLoginTime { get; set; }


        public static ApplicationUser ToEntity(ApplicationUserViewModel m)
        {
            var user = new ApplicationUser
            {
                Id = m.Id,
                UserName = m.UserName,
                Name = m.Name,
                Email = m.Email,
                PhoneNumber = m.PhoneNumber,
                DepartmentId = m.DepartmentId,
                CreateTime = m.CreateTime,
                LastLoginTime = m.LastLoginTime
            };
            return user;
        }

        public static ApplicationUserViewModel ToModel(ApplicationUser u)
        {
            var model = new ApplicationUserViewModel
            {
                Id = u.Id,
                UserName = u.UserName,
                Name = u.Name,
                Email = u.Email,
                PhoneNumber = u.PhoneNumber,
                DepartmentId = u.DepartmentId,
                CreateTime = u.CreateTime,
                LastLoginTime = u.LastLoginTime
            };
            return model;
        }
    }
}
